package view;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import darksideoftheloom.Attendee;
import darksideoftheloom.Submission;
import darksideoftheloom.WeavingDraft;

/**
 * This class provides a submission form for contest entries.
 * 
 * @author Shane Kwon
 * @version 2013-03-09
 * 
 */
@SuppressWarnings("serial")
public class BeweaveSubmitPanel extends JPanel {

    /**
     * The width of the panel.
     */
    private static final int WIDTH = 1024;

    /**
     * The height of the panel.
     */
    private static final int HEIGHT = 768;

    /**
     * The index number of the BeweaveAttendeePanel in the CardLayout panel.
     */
    private static final int BAPINDEX = 3;

    /**
     * An array of categories.
     */
    // TODO real categories
    private final String[] categories = { "Category 1", "Category 2",
            "Category 3" };

    /**
     * The attendee whose submissions will be modified.
     */
    private Attendee my_attendee;

    /**
     * The text field for the design title.
     */
    private final JTextField title_field = new JTextField();
    
    /**
     * Reference to the weaving draft panel.
     */
    private final BeweaveWeaveDraftPanel my_draft_panel;

    /**
     * The list of categories.
     */
    private JList category_list;

    /**
     * The default width in number of columns for the title field.
     */
    private static final int COLS = 20;

    /**
     * Constructs a form that the user can fill out to submit a contest entry.
     * 
     * @param the_panel
     *            the panel container, used for switching between panels
     */
    public BeweaveSubmitPanel(final JPanel the_panel, final BeweaveWeaveDraftPanel the_draft_panel) {
        super();
        setLayout(null);
        my_draft_panel = the_draft_panel;

        JLabel designname_label = new JLabel("Design name: ");
        JLabel category_label = new JLabel("Category: ");
        JLabel draft_label = new JLabel("Weaving Draft: ");
        category_list = new JList(categories);
        title_field.setColumns(COLS);
        // TODO weaving draft issue design
        final WeavingDraft wd = null;
        final JButton draft_button = new JButton(new AbstractAction(
                "Upload weaving draft...") {
            public void actionPerformed(final ActionEvent the_event) {
                CardLayout cl = (CardLayout) the_panel.getLayout();
                cl.show(the_panel, "draft");
            }
        });
        // draft_button.setEnabled(false);

        final JButton submit_button = new JButton(new AbstractAction("Submit") {
            public void actionPerformed(final ActionEvent the_event) {
                // TODO hard coded value...
                final Submission s = new Submission("test", title_field
                        .getText(), (String) category_list.getSelectedValue(),
                        my_draft_panel.getDraft());
                my_draft_panel.refresh();
                my_attendee.submit(s);
                CardLayout cl = (CardLayout) the_panel.getLayout();
                ((BeweaveAttendeePanel) the_panel.getComponent(BAPINDEX))
                        .showSubmissions();
                cl.show(the_panel, "attendee");
                // TODO set fields to default values
                my_attendee = null;
                title_field.setText("");
                category_list.setSelectedIndex(0);
            }
        });

        add(designname_label);
        add(title_field);
        add(category_label);
        add(category_list);
        add(draft_label);
        add(draft_button);
        add(submit_button);

        // TODO get rid of hardcoded values
        Insets insets = getInsets();
        Dimension size = designname_label.getPreferredSize();
        designname_label.setBounds(325 + insets.left, 300 + insets.top,
                size.width, size.height);
        size = category_label.getPreferredSize();
        category_label.setBounds(325 + insets.left, 325 + insets.top,
                size.width, size.height);
        size = draft_label.getPreferredSize();
        draft_label.setBounds(325 + insets.left, 385 + insets.top, size.width,
                size.height);

        size = title_field.getPreferredSize();
        title_field.setBounds(500 + insets.left, 300 + insets.top, size.width,
                size.height);
        size = category_list.getPreferredSize();
        category_list.setBounds(500 + insets.left, 325 + insets.top,
                size.width, size.height);
        size = draft_button.getPreferredSize();
        draft_button.setBounds(500 + insets.left, 385 + insets.top, size.width,
                size.height);

        size = submit_button.getPreferredSize();
        submit_button.setBounds(642 + insets.left, 450 + insets.top,
                size.width, size.height);

        setPreferredSize(new Dimension(WIDTH, HEIGHT));
    }

    /**
     * Sets the Attendee whose submissions will be modified by this class.
     * 
     * @param the_attendee
     *            the Attendee whose submissions will be modified by this class.
     */
    public void setAttendee(final Attendee the_attendee) {
        my_attendee = the_attendee;
    }
}
